package json;

import com.google.common.collect.Sets;
import com.google.gson.*;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 * @Author: ycy
 * @CreateDate: 2019/9/20 17:05
 */
public class GSONDemo {

  public static void main(String[] args) {
    String json = "{\"total\":376,\"time\":5,\"hits\":[{\"id\":\"69355386\",\"word\":\"天御国际广场中心广场\",\"type\":\"gsmarkland\",\"eName\":\"\",\"commentCount\":\"0\",\"commentScore\":\"0.0\",\"districtId\":\"2\",\"districtName\":\"上海\",\"pinyin\":\"\",\"cityId\":\"2\",\"cityName\":\"\",\"countryId\":\"110000\",\"countryName\":\"中国\",\"lat\":\"31.22312\",\"lon\":\"121.37954\",\"poiId\":\"69355386\",\"alias\":\"\",\"luceneScore\":\"1.7320508\",\"address\":\"丹巴路99\",\"imageUrl\":\"\"},{\"id\":\"69142240\",\"word\":\"上海长风视觉艺术馆\",\"type\":\"gsmarkland\",\"eName\":\"\",\"commentCount\":\"0\",\"commentScore\":\"0.0\",\"districtId\":\"2\",\"districtName\":\"上海\",\"pinyin\":\"\",\"cityId\":\"2\",\"cityName\":\"\",\"countryId\":\"110000\",\"countryName\":\"中国\",\"lat\":\"31.220686\",\"lon\":\"121.379406\",\"poiId\":\"69142240\",\"alias\":\"\",\"luceneScore\":\"1.7320508\",\"address\":\"光复西路2690号\",\"imageUrl\":\"\"},{\"id\":\"69358505\",\"word\":\"红森武道馆\",\"type\":\"gsmarkland\",\"eName\":\"\",\"commentCount\":\"0\",\"commentScore\":\"0.0\",\"districtId\":\"2\",\"districtName\":\"上海\",\"pinyin\":\"\",\"cityId\":\"2\",\"cityName\":\"\",\"countryId\":\"110000\",\"countryName\":\"中国\",\"lat\":\"31.221891\",\"lon\":\"121.38247\",\"poiId\":\"69358505\",\"alias\":\"\",\"luceneScore\":\"1.7320508\",\"address\":\"云岭东路599弄5号楼\",\"imageUrl\":\"\"}],\"executedQuery\":\"+(+type:gsmarkland) +(+(+citypath:2) +(+GeoPointDistanceQuery: field=_lon_lat: Center: [121.37989807128906,31.222000122070312] Distance: 3000.0 meters]))\"}";
    MarklandResultEntity marklandResultEntity = new Gson().fromJson(json, MarklandResultEntity.class);
    System.out.println(marklandResultEntity);
  }

  @Test
  public void test1() {
    try {
      String mapping = FileUtils.readFileToString(new File("E:\\linshi\\20200617\\globalsearch_mapping.json"), StandardCharsets.UTF_8);

      JsonParser parser = new JsonParser();
      JsonObject mappingjson = parser.parse(mapping).getAsJsonObject();

      Map<String, String> mapjson_new = new HashMap<>();

      Gson gson = new GsonBuilder().create();

      Map<String, Object> stringObjectMap = toMap(mappingjson);
      for (Map.Entry<String, Object> stringObjectEntry : stringObjectMap.entrySet()) {
        mapjson_new.put(stringObjectEntry.getKey().toLowerCase(), gson.toJson(stringObjectEntry.getValue()));
      }

      for (Map.Entry<String, String> stringStringEntry : mapjson_new.entrySet()) {
        System.out.println(stringStringEntry + "--");
      }

      List<String> strings = FileUtils.readLines(new File("E:\\linshi\\20200617\\main.txt"), StandardCharsets.UTF_8);

      for (String string : strings) {
        String key = string.toLowerCase();
        String s = mapjson_new.get(key);
        System.out.println("\"" + key + "\"" + ":" + s + ",");
      }

    } catch (IOException e) {
      e.printStackTrace();
    }

  }

  @Test
  public void test3() {

    String marketingLabelCode = "{\"cjppr\":\"超级品牌日\",\"cj\":\"国庆\"}";
    JsonParser JSON_PARSER = new JsonParser();
    JsonObject asJsonObject = JSON_PARSER.parse(marketingLabelCode).getAsJsonObject();
    Set<Map.Entry<String, JsonElement>> entries = asJsonObject.entrySet();
    Set<String> marketingLabels = Sets.newHashSet();
    for (Map.Entry<String, JsonElement> entry : entries) {
      JsonElement value = entry.getValue();
      String marketingLabelName = value.getAsString();
      marketingLabels.add(marketingLabelName);
    }
    System.out.println(StringUtils.join(marketingLabels.iterator(), ","));
  }

  /**
   * 〈简述〉将JSONObjec对象转换成Map-List集合
   * 〈详细描述〉
   *
   * @param json json对象
   * @return Map<String, Object> map集合
   */
  public static Map<String, Object> toMap(JsonObject json) {
    Map<String, Object> map = new HashMap<String, Object>();
    Set<Map.Entry<String, JsonElement>> entrySet = json.entrySet();
    for (Iterator<Map.Entry<String, JsonElement>> iter = entrySet.iterator(); iter.hasNext(); ) {
      Map.Entry<String, JsonElement> entry = iter.next();
      String key = entry.getKey();
      Object value = entry.getValue();
      if (value instanceof JsonArray)
        map.put((String) key, toList((JsonArray) value));
      else if (value instanceof JsonObject)
        map.put((String) key, toMap((JsonObject) value));
      else
        map.put((String) key, value);
    }
    return map;
  }

  /**
   * 〈简述〉将JSONArray对象转换成List集合
   * 〈详细描述〉
   *
   * @param json jsonArray对象
   * @return List<Object>
   */
  public static List<Object> toList(JsonArray json) {
    List<Object> list = new ArrayList<Object>();
    for (int i = 0; i < json.size(); i++) {
      Object value = json.get(i);
      if (value instanceof JsonArray) {
        list.add(toList((JsonArray) value));
      } else if (value instanceof JsonObject) {
        list.add(toMap((JsonObject) value));
      } else {
        list.add(value);
      }
    }
    return list;
  }

  @Test
  public void test2() {
    Map<String, Object> map = new HashMap<>();
    map.put("i", 1);
    map.put("do", null);
    Gson gson = new GsonBuilder().serializeNulls().create();
    String s = gson.toJson(map);
    System.out.println(s);

    Gson gson1 = new GsonBuilder().create();
    Map map1 = gson1.fromJson(s, Map.class);
    System.out.println(map1);
  }

}



